package blog

import (
	"crypto/md5"
	"fmt"
	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
	"net/http"
)

var Db *sqlx.DB
var Db2 *sqlx.DB

func init() {
	database, err := sqlx.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/test_table")

	if err != nil {
		fmt.Println("open mysql failed", err)
		//defer Db.Close()
		return
	}
	Db = database
}
func init() {
	database, err := sqlx.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/go")

	if err != nil {
		fmt.Println("open mysql failed", err)
		//defer Db.Close()
		return
	}
	Db2 = database
}

type Task struct {
	ProjectName string `db:"project_name"`
	DagId       string `db:"dag_id"`
	TaskId      string `db:"task_id"`
	FuncName    string `db:"func_name"`
}

type AUser struct {
	// binding:"required"修饰的字段，若接收为空值，则报错，是必须字段
	User     string `form:"user" json:"user" uri:"user" xml:"user" binding:"required"`
	Password string `form:"password" json:"password" uri:"password" xml:"password" binding:"required"`
}

func calcMD5(message string) string {
	data := []byte(message)
	return fmt.Sprintf("%x", md5.Sum(data))
}

func dataShow(c *gin.Context) {
	var task []Task
	err := Db.Select(&task, "select project_name, dag_id, task_id, func_name from airflow_task_func")
	if err != nil {
		fmt.Println("exec failed: ", err)
	}
	//for _, i := range task {
	//	fmt.Println(i)
	//	fmt.Println(i.ProjectName)
	//}
	//fmt.Println("success : ", task)
	c.JSON(200, task)
	//c.String(200, "aaa", task)
}

//注册
func register(c *gin.Context) {
	var json AUser
	//需要用改函数对穿过来的数据进行解析
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	}
	//1、检测账号是否被注册
	var userInfo []AUser
	err := Db2.Select(&userInfo, "select user, password from auser where user=?", json.User)
	fmt.Println(userInfo, err)
	if err != nil {
		fmt.Println("数据库问题，清稍后再试")
		c.JSON(203, gin.H{"status": 203, "message": "数据库问题，清稍后再试"})
		return
	}

	if userInfo != nil {
		fmt.Println(err)
		fmt.Println("账号已经被注册")
		c.JSON(203, gin.H{"status": 203, "message": "账号已经被注册"})
		return
	}
	fmt.Println(json.Password, json.User)
	r, err := Db2.Exec("insert into auser(user, password)values(?, ?)", json.User, json.Password)
	if err != nil {
		fmt.Println("exec failed, ", err)
		c.JSON(203, gin.H{"status": 203, "message": err})
		return
	}
	id, err2 := r.LastInsertId()
	if err2 != nil {
		fmt.Println("exec failed, ", err)
		c.JSON(203, gin.H{"status": 203, "message": err})
		return
	}
	fmt.Println("insert succ:", id)
	c.JSON(200, gin.H{"status": "200"})

}

//登录
func login(c *gin.Context) {
	var json AUser
	//需要用改函数对穿过来的数据进行解析
	if err := c.ShouldBindJSON(&json); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
	}
	//接收表单数据
	//if err := c.Bind(&json); err != nil {
	//	c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	//}
	//if err := c.ShouldBindUri(&json); err != nil {
	//	c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	//}
	//检测账号密码是否正确
	var userInfo []AUser
	err := Db2.Select(&userInfo, "select password from auser where user=?", json.User)
	fmt.Println("aaa", err, userInfo)
	if err != nil || userInfo == nil {
		fmt.Println("账号或者密码不正确")
		c.JSON(203, gin.H{"status": 203, "message": "账号或者密码不正确"})
	} else {
		fmt.Println()
		var md5Password = userInfo[0].Password
		//将密码转换成md5格式密码与数据库密码进行对比
		var a = calcMD5(json.Password + "leshen")
		fmt.Println(md5Password, a)
		if md5Password == a {
			c.JSON(200, gin.H{"status": "200"})
		} else {
			fmt.Println("账号或者密码不正确2")
			c.JSON(203, gin.H{"status": 203, "message": "账号或者密码不正确"})
		}
	}

}
